package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.Merchant;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.entity.UserAppraise;
import com.rm.zeyou.entity.UserAttention;
import com.rm.zeyou.mapper.*;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.MerchantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.service.TechnicianService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.distance.MathDistanceUtil;
import com.rm.zeyou.vo.request.SelectMerchantVo;
import com.rm.zeyou.vo.response.SelectMerchantResponseVo;
import com.rm.zeyou.vo.response.SelectTechnicianListResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商家表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:08
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {
    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private TechnicianMapper technicianMapper;
    @Resource
    private TechnicianService technicianService;
    @Resource
    private TechnicianTypeMapper technicianTypeMapper;
    @Resource
    private UserAppraiseMapper userAppraiseMapper;
    @Resource
    private UserAttentionMapper userAttentionMapper;

    @Override
    public ApiRestResponse<SelectMerchantResponseVo> selectMerchant(SelectMerchantVo requestVo) {
        Merchant merchant = merchantMapper.selectById(requestVo.getId());
        SelectMerchantResponseVo responseVo = new SelectMerchantResponseVo();
        responseVo.setMerchantName(merchant.getMerchantName());
        responseVo.setMerchantLicense(merchant.getMerchantLicense());
        responseVo.setMerchantOrderRate(merchant.getMerchantOrderRate());
        responseVo.setMerchantFavorableRate(merchant.getMerchantFavorableRate());


        List<Technician> technicianList = technicianMapper.selectList(new LambdaQueryWrapper<Technician>()
                .eq(Technician::getMerchantId, requestVo.getId()));

        List<SelectTechnicianListResponseVo> voList = new ArrayList<>();
        if (!CheckEmptyUtil.isEmpty(technicianList)) {
            PageInfo<Technician> source = new PageInfo<>(technicianList);
            PageInfo<SelectTechnicianListResponseVo> target = new PageInfo<>();
            BeanUtils.copyProperties(source, target);
            source.getList().forEach(item -> {
                SelectTechnicianListResponseVo vo = new SelectTechnicianListResponseVo();
                vo.setId(item.getId());
                vo.setTechnicianAvatar(item.getTechnicianAvatar());
                vo.setTechnicianName(item.getTechnicianName());

                vo.setTechnicianAge(technicianService.selectTechnicianAge(item));
                vo.setTechnicianSex(item.getTechnicianSex());
                vo.setMark(item.getMark());
                vo.setOrderReceiving(technicianService.selectTechnicianOrderReceiving(item)+item.getOrderReceiving());
                String distance = MathDistanceUtil.getDistance(requestVo.getLongitude() + "," + requestVo.getLatitude(), item.getLongitude() + "," + item.getLatitude(), 2);
                BigDecimal distanceBig = new BigDecimal(distance).divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP);
                vo.setDistance(distanceBig.toPlainString());
                vo.setMerchantName(merchant.getMerchantName());
                int technicianAppraiseCount = userAppraiseMapper.selectCount(new LambdaQueryWrapper<UserAppraise>()
                        .eq(UserAppraise::getTid, item.getId())).intValue();
                vo.setCommentCount(technicianAppraiseCount);

                int technicianAttentionCount = userAttentionMapper.selectCount(new LambdaQueryWrapper<UserAttention>()
                        .eq(UserAttention::getTid, item.getId())).intValue();
                vo.setAttentionCount(technicianAttentionCount);
                vo.setEarliestTime(technicianService.selectTechnicianEarliesTime(item.getId()));
                vo.setTechnicianType(technicianTypeMapper.selectById(item.getTechnicianType()).getTypeName());
                voList.add(vo);
            });
            target.setList(voList);
            responseVo.setTechnicianList(target);
        } else {
            responseVo.setTechnicianList(new PageInfo<>(voList));
        }
        return ApiRestResponse.success(responseVo);
    }
}
